"""
    @TODO 时间工具类
"""
from datetime import datetime, timedelta
from time import time

DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
DATETIME_FORMAT_YMD_HM = '%Y-%m-%d %H:%M'
DATETIME_FORMAT_YM = '%Y-%m'
DATETIME_FORMAT_YMD = '%Y-%m-%d'

NO_ORDER = '%Y%m%d'


def get_now_time_stamp():
    """
    获取现在时间戳
    :return:
    """
    return time()


def format_date(date, format):
    """
    时间格式化
    :param date:
    :param format:
    :return:
    """
    return date.strftime(format)


def get_now_date():
    """
    获取现在时间
    :return:
    """
    return datetime.now()


def get_now_day_zero():
    """
    获取0点
    :return:
    """
    return get_date_day_zero(date=get_now_date())


def get_date_day_zero(date):
    """
     获取0点
    :param date:
    :return:
    """
    return datetime(date.year, date.month, date.day, 0, 0, 0)


def get_now_n_day(days=0):
    """
    获取n天前后的时间 0:0:0
    :param days:
    :return:
    """
    return get_date_n_day(date=get_now_date(), days=days)


def get_date_n_day(date, days=0):
    """
    获取n天前后的时间 0:0:0
    :param date:
    :param days:
    :return:
    """
    _date = date + timedelta(days=days)
    return datetime(_date.year, _date.month, _date.day, 0, 0, 0)


def get_now_n_monthly_last_day(monthly=0):
    """
    获取现在时间n个月前后的时间最后一天 0:0:0
    :param monthly:
    :return:
    """
    _date = get_now_n_monthly(monthly=0)
    return get_date_n_day(date=_date, days=-1)


def get_now_n_monthly(monthly=0):
    """
    获取现在时间n个月前后的时间
    :param monthly:
    :return:
    """
    _date = get_now_date()
    return get_date_n_monthly(date=_date, monthly=monthly)


def get_date_n_monthly(date, monthly=0):
    """
    获取传入时间n个月前后的时间
    :param date:
    :param monthly:
    :return:
    """
    _year = date.year
    _month = date.month
    _total_month = _month + monthly

    if (monthly >= 0):
        # 获取之后的
        if (_total_month <= 12):
            return datetime(_year, _total_month, 1, 0, 0, 0)
        else:
            i = int(_total_month / 12)
            j = int(_total_month % 12)
            if (j == 0):
                i -= 1
                j = 12
            return datetime(_year + i, j, 1, 0, 0, 0)
    else:
        # 获取之前
        if ((_total_month > 0) and (_total_month < 12)):
            return datetime(_year, _total_month, 1, 0, 0, 0)
        else:
            i = int(_total_month / 12)
            j = int(_total_month % 12)
            if (j == 0):
                i -= 1
                j = 12
            return datetime(_year + i, j, 1, 0, 0, 0)
